package com.welyo.view {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.system.Security;
	
	/**
	 * ...
	 * @author vu.doan
	 */
	public class SoundContent extends Sprite {
		
		private var isTurnOff		:Boolean = false;
		private var sound			:Sound;
		private var sTransform		:SoundTransform;
		private var sChannel		:SoundChannel;
		
		public function SoundContent() {
			Security.allowDomain("*");
			//var vo:SharedObject = SharedObject.getLocal("SoundStatus", "/");
			//
			//if (vo.data["welyoSoundStatus"]) {
				//isTurnOff = (vo.data["welyoSoundStatus"] == "1");
			//}
			//
			//if (isTurnOff) {
				//mcSoundIcon.gotoAndStop(2);
			//} else {
				//mcSoundIcon.gotoAndStop(1);
			//}
			//
			var path:String = "";
			if (loaderInfo.parameters.soundpath) {
				path = loaderInfo.parameters.soundpath;
			}
			
			mcSoundIcon.buttonMode = true;
			mcSoundIcon.addEventListener(MouseEvent.CLICK, onClickButtonHandler);
			
			sTransform = new SoundTransform();
			sTransform.volume = 0.85;
			
			sound = new Sound();
			sound.load(new URLRequest(path));
			
			sChannel = sound.play(0, 10000);
			if (sChannel) {
				sChannel.soundTransform = sTransform;
			}
			
			if (ExternalInterface.available) {
				ExternalInterface.addCallback("turnOffSound", turnOffSound);
				ExternalInterface.addCallback("turnOnSound", turnOnSound);
			}
		}
		
		public function turnOffSound():void {
			sTransform = new SoundTransform();
			sTransform.volume = 0;
			
			if (sChannel) {
				sChannel.soundTransform = sTransform;
			}
			
			isTurnOff = true;
			mcSoundIcon.gotoAndStop(2);
			ExternalInterface.call("saveStatus", 0);
		}
		
		public function turnOnSound():void {
			sTransform = new SoundTransform();
			sTransform.volume = 0.85;
			
			if (sChannel) {
				sChannel.soundTransform = sTransform;
			}
			
			isTurnOff = false;
			mcSoundIcon.gotoAndStop(1);
			ExternalInterface.call("saveStatus", 1);
		}
		
		private function onClickButtonHandler(evt:MouseEvent):void {
			if (isTurnOff) {
				//var vo:SharedObject = SharedObject.getLocal("SoundStatus", "/");
				//vo.data["welyoSoundStatus"] = "1";
				
				ExternalInterface.call("saveStatus", 1);
				
				isTurnOff = false;
				mcSoundIcon.gotoAndStop(1);
				sTransform = new SoundTransform();
				sTransform.volume = 0.85;
				
				if (sChannel) {
					sChannel.soundTransform = sTransform;
				}
			} else {
				//var vo:SharedObject = SharedObject.getLocal("SoundStatus", "/");
				//vo.data["welyoSoundStatus"] = "0";
				
				ExternalInterface.call("saveStatus", 0);
				
				isTurnOff = true;
				mcSoundIcon.gotoAndStop(2);
				
				sTransform = new SoundTransform();
				sTransform.volume = 0;
				
				if (sChannel) {
					sChannel.soundTransform = sTransform;
				}
			}
		}
	}
}